package com.lza.phone.mapper;

import com.lza.phone.entity.Phone;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lza.phone.entity.connectEntity.PhoneList;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 李钊澳
 * @since 2021-08-28
 */

public interface PhoneMapper extends BaseMapper<Phone> {
    /*
 @Result(property = "user", //要封装的属性名称 PhoneList.user
 		 column = "uid", //根据哪个字段去查询user表的数据(数据库里面的字段)
         javaType = User.class, //要封装的实体类型
         //select属性 代表查询哪个接口的方法获得数据
         one = @One(select = "come.lza.mapper.UserMapper.findById"))
*/
    //联表的这波操作必须搞,全部是·一对一联表联查
    @Results({
            //这几个id没有就没有输出
            @Result(column = "screenId",property = "screenId"),
            @Result(column = "sizeId",property = "sizeId"),
            @Result(column = "batteryId",property = "batteryId"),
            @Result(column = "cameraId",property = "cameraId"),
            @Result(column = "socId",property = "socId"),
            @Result(column = "performanceId",property = "performanceId"),
            @Result(column = "batteryId",property = "battery",one = @One(select = "com.lza.phone.mapper.BatteryMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "cameraId",property = "camera",one = @One(select = "com.lza.phone.mapper.CameraMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "PerformanceId",property = "performance",one = @One(select = "com.lza.phone.mapper.PerformanceMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "ScreenId",property = "screen",one = @One(select = "com.lza.phone.mapper.ScreenMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SizeId",property = "size",one = @One(select = "com.lza.phone.mapper.SizeMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SocId",property = "soc",one = @One(select = "com.lza.phone.mapper.SocMapper.selectById",fetchType= FetchType.EAGER))
    })
    @Select("select * from phone where id=#{id}")
    PhoneList getOnePhone(@Param("id") Integer id);
    //联表的这波操作必须搞
    @Results({
            //这几个id没有就没有输出
            @Result(column = "screenId",property = "screenId"),
            @Result(column = "sizeId",property = "sizeId"),
            @Result(column = "batteryId",property = "batteryId"),
            @Result(column = "cameraId",property = "cameraId"),
            @Result(column = "socId",property = "socId"),
            @Result(column = "performanceId",property = "performanceId"),

            @Result(column = "batteryId",property = "battery",one = @One(select = "com.lza.phone.mapper.BatteryMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "cameraId",property = "camera",one = @One(select = "com.lza.phone.mapper.CameraMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "PerformanceId",property = "performance",one = @One(select = "com.lza.phone.mapper.PerformanceMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "ScreenId",property = "screen",one = @One(select = "com.lza.phone.mapper.ScreenMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SizeId",property = "size",one = @One(select = "com.lza.phone.mapper.SizeMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SocId",property = "soc",one = @One(select = "com.lza.phone.mapper.SocMapper.selectById",fetchType= FetchType.EAGER))
    })
    @Select("select * from phone")
    List<PhoneList> getAllPhone();

   //按照cpu分数排名排序
    @Results({
            //这几个id没有就没有输出
            @Result(column = "screenId",property = "screenId"),
            @Result(column = "sizeId",property = "sizeId"),
            @Result(column = "batteryId",property = "batteryId"),
            @Result(column = "cameraId",property = "cameraId"),
            @Result(column = "socId",property = "socId"),
            @Result(column = "performanceId",property = "performanceId"),

            @Result(column = "batteryId",property = "battery",one = @One(select = "com.lza.phone.mapper.BatteryMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "cameraId",property = "camera",one = @One(select = "com.lza.phone.mapper.CameraMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "PerformanceId",property = "performance",one = @One(select = "com.lza.phone.mapper.PerformanceMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "ScreenId",property = "screen",one = @One(select = "com.lza.phone.mapper.ScreenMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SizeId",property = "size",one = @One(select = "com.lza.phone.mapper.SizeMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SocId",property = "soc",one = @One(select = "com.lza.phone.mapper.SocMapper.selectById",fetchType= FetchType.EAGER))
    })
    //数据库是phone 这边是从phoneList里面查询,里面包含b.*,c.*,per.*,scr.*.......,因此不用加上
    @Select("select p.* \n" +
            "from phone p\n" +
            "LEFT JOIN battery b\n" +
            "on\n" +
            "p.id=b.batteryId\n" +
            "LEFT JOIN camera c\n" +
            "on\n" +
            "p.id=c.cameraId\n" +
            "LEFT JOIN performance per\n" +
            "on\n" +
            "p.id=per.performanceId\n" +
            "LEFT JOIN screen s\n" +
            "on\n" +
            "p.id=s.screenId\n" +
            "LEFT JOIN size si\n" +
            "on\n" +
            "p.id=si.sizeId\n" +
            "LEFT JOIN soc so\n" +
            "on\n" +
            "p.id=so.socId\n" +
            "ORDER BY\n" +
            "per.cpuPkMark Desc\n")
    List<PhoneList> getAllPhoneByCpuMark();



    //第二波实现方法(完美方法)
    @Results({
            @Result(column = "batteryId",property = "battery",one = @One(select = "com.lza.phone.mapper.BatteryMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "cameraId",property = "camera",one = @One(select = "com.lza.phone.mapper.CameraMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "PerformanceId",property = "performance",one = @One(select = "com.lza.phone.mapper.PerformanceMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "ScreenId",property = "screen",one = @One(select = "com.lza.phone.mapper.ScreenMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SizeId",property = "size",one = @One(select = "com.lza.phone.mapper.SizeMapper.selectById",fetchType= FetchType.EAGER)),
            @Result(column = "SocId",property = "soc",one = @One(select = "com.lza.phone.mapper.SocMapper.selectById",fetchType= FetchType.EAGER))
    })
    @Select("select p.*\n" +
            "from phone p\n" +
            "LEFT JOIN battery b\n" +
            "on\n" +
            "p.id=b.batteryId\n" +
            "LEFT JOIN camera c\n" +
            "on\n" +
            "p.id=c.cameraId\n" +
            "LEFT JOIN performance per\n" +
            "on\n" +
            "p.id=per.performanceId\n" +
            "LEFT JOIN screen s\n" +
            "on\n" +
            "p.id=s.screenId\n" +
            "LEFT JOIN size si\n" +
            "on\n" +
            "p.id=si.sizeId\n" +
            "LEFT JOIN soc so\n" +
            "on\n" +
            "p.id=so.socId\n" +
            "ORDER BY\n" +
            "per.cpuPkMark Desc\n")
    List<PhoneList> AllPhone();
}
